package 自己写一个json解析器测试;

public class 词法分析 {

    private final CharReader charReader;
    private final TokenList tokenList;

    public 词法分析(String strJson) {
        this.charReader = new CharReader(strJson);
        this.tokenList = new TokenList();
        tokenizer();
    }

    // 分词器
    private void tokenizer() {
        Token token;
        do {
            token = start();
            tokenList.add(token);
        } while (token.getTokenType() != TokenType.END_DOCUMENT);

        语法解析 语法解析 = new 语法解析();
        JsonObject parse = (JsonObject) 语法解析.parse(tokenList);
        JsonObject jsonParse = (JsonObject) parse.get(null);

        System.out.println();
    }


    /**
     * 解析过程的具体实现方法
     */
    private Token start() {
        char ch;
        while (true) {
            if (!charReader.hasMore()) {
                return new Token(TokenType.END_DOCUMENT, null);
            }

            ch = charReader.next();

            switch (ch) {
                case '{':
                    return new Token(TokenType.开始对象, String.valueOf(ch));
                case '}':
                    return new Token(TokenType.结束对象, String.valueOf(ch));
                case '[':
                    return new Token(TokenType.开始数组, String.valueOf(ch));
                case ']':
                    return new Token(TokenType.结束数组, String.valueOf(ch));
                case ',':
                    return new Token(TokenType.逗号, String.valueOf(ch));
                case ':':
                    return new Token(TokenType.冒号, String.valueOf(ch));
              case 'n': // null
                    return readNull();
                 case 't': // true
                case 'f': // false
                    return readBoolean();
                case '"':
                    return readString();
            }
        }
    }

    private Token readBoolean() {

        if (charReader.current() == 't') {
            boolean r = charReader.next() == 'r';
            boolean u = charReader.next() == 'u';
            boolean e = charReader.next() == 'e';
            if (!(r && u && e)) {
                System.out.println("不是true");
                return null;
            }
            return new Token(TokenType.布尔值, "true");
        } else {
            boolean a = charReader.next() == 'a';
            boolean l = charReader.next() == 'l';
            boolean s = charReader.next() == 's';
            boolean e = charReader.next() == 'e';
            if (!(a && l && s && e)) {
                System.out.println("不是false");
                return null;
            }

            return new Token(TokenType.布尔值, "false");
        }
    }

    private Token readNull() {
        boolean u = charReader.next() == 'u';
        boolean l1 = charReader.next() == 'l';
        boolean l2 = charReader.next() == 'l';

        if (!(u && l1 && l2)) {
            System.out.println("不是null");
        }

        return new Token(TokenType.NULL, "null");

    }

    private Token readString()  {
        StringBuilder sb = new StringBuilder();
        while(true) {
            char ch = charReader.next();
            if (ch == '"') {     // 碰到另一个双引号，则认为字符串解析结束，返回 Token
                return new Token(TokenType.字符串, sb.toString());
            }

            sb.append(ch);
        }
    }
}
